package com.example.treasure_back.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.treasure_back.enumerate.CoinChangeType;
import com.example.treasure_back.mapper.CoinChangeMapper;
import com.example.treasure_back.mapper.UserMapper;
import com.example.treasure_back.model.CoinChangeModel;
import com.example.treasure_back.model.UserModel;
import com.example.treasure_back.model.vo.CoinViewModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CoinService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    GoodsService goodsService;
    @Autowired
    CoinChangeMapper coinChangeMapper;
    @Autowired
    ExchangeRecordService exchangeRecordService;

    public Integer getCoinByEmail(String email){
        UserModel userModel = userMapper.selectById(email);
        return userModel.getCoin();
    }

    public String reduceCoinByEmail(String email, Integer id){
        UserModel userModel = userMapper.selectById(email);
        Integer coin = goodsService.getGoodsPriceById(id);
        Integer number = goodsService.getGoodsNumberById(id);
        int now_coin = userModel.getCoin();
        // 积分数目不够，后者商品数目为0
        if(now_coin<coin){
            return "-1";
        }
        else if(number == 0){
            return "-2";
        }
        // 更新用户积分
        int new_coin = now_coin - coin;
        UpdateWrapper<UserModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("coin", new_coin);
        updateWrapper.eq("email", email);
        userMapper.update(userModel,updateWrapper);
        // 减少商品数量
        goodsService.reduceGoodsById(id);
        // 插入积分记录
        insertGoodsRecord(email,id, new_coin);
        // 插入兑换记录
        exchangeRecordService.insertExchangeRecord(email,id);
        return "200";
    }

    public void insertGoodsRecord(String email, Integer goodsId,Integer new_coin){
        Integer price = goodsService.getGoodsPriceById(goodsId);
        CoinChangeModel coinChangeModel = new CoinChangeModel();
        coinChangeModel.setChangeNum(-price);
        coinChangeModel.setEmail(email);
        coinChangeModel.setGoodsId(goodsId);
        coinChangeModel.setReason(CoinChangeType.BUY_GOODS);
        coinChangeModel.setCoin(new_coin);
        coinChangeMapper.insert(coinChangeModel);
    }

    public List<CoinViewModel> getCoinView(String email){
        QueryWrapper<CoinChangeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",email);
        queryWrapper.orderByDesc("create_time");
        List<CoinChangeModel> coinChangeModels = coinChangeMapper.selectList(queryWrapper);
        ArrayList<CoinViewModel> coinViewModels = new ArrayList<>();
        for (CoinChangeModel coinChangeModel : coinChangeModels) {
            CoinViewModel coinViewModel = new CoinViewModel();
            coinViewModel.setId(coinChangeModel.getId());
            coinViewModel.setChangeNum(coinChangeModel.getChangeNum());
            coinViewModel.setCoin(coinChangeModel.getCoin());
            coinViewModel.setCreateTime(coinChangeModel.getCreateTime().toString());
            if (coinChangeModel.getReason() == CoinChangeType.BUY_GOODS){
                String name = goodsService.getGoodsNameById(coinChangeModel.getGoodsId());
                coinViewModel.setReason("兑换" + name);
            }
            else {
                coinViewModel.setReason(String.valueOf(coinChangeModel.getReason()));
            }
            coinViewModels.add(coinViewModel);
        }
        return coinViewModels;
    }

    public Boolean deleteCoinChangeRecord(String email, Integer id){
        QueryWrapper<CoinChangeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        queryWrapper.eq("id", id);
        CoinChangeModel coinChangeModel = coinChangeMapper.selectOne(queryWrapper);
        if (coinChangeModel == null){
            return false;
        }
        else{
            coinChangeMapper.delete(queryWrapper);
            return true;
        }
    }


}
